﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    public class StudentController : Controller
    {
        #region 学生列表视图 + ActionResult index()
        /// <summary>
        /// 学生列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult index()
        {
            return View();
        } 
        #endregion

        #region 获取学生列表数据 +GetStudentData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取学生列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetStudentData()
        {
            
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetPagedList(pageIndex, pageSize, x => x.StudentId != null, m => m.StudentId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentId != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改学生 window 的html + EditStudent()
        /// <summary>
        /// 加载修改学生 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditStudent(int id)
        {
            SetDropDonwList();
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改学生信息 EditStudent(MODEL.qgzx_student model)
        /// <summary>
        ///post修改学生信息 EditStudent(MODEL.qgzx_student model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditStudent(MODEL.qgzx_student_information model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.Modify(model, "StudentId", "StudentName", "StudentNum", "Gender", "Grade", "password", "Profession", "College", "Degree", "EducationYear", "Disclipline", "Class", "Instructor", "InstructorPhone", "Nation", "PoliticStatus", "StudentSource", "IdCard", "BankCard", "BankCard", "SchoolCard", "Telephone", "ShortPhone", "Email", "QQ", "Dormitory", "Height", "Age", "Interest", "RemarkDetail");
            if (res > 0)
            {
                return Redirect("/admin/student/index");
            }
            return Redirect("/admin/student/index");
        }
        #endregion

        #region 在这新增学生员 html代码 + AddStudent()
        /// <summary>
        /// 在这新增学生员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddStudent()
        {

            //SetDropDonwList();
            return PartialView("EditStudent");
        }
        #endregion

        #region 在这新增学生员 html代码 + AddStudent(MODEL.qgzx_student_information model)
        /// <summary>
        /// 在这新增学生员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddStudent(MODEL.qgzx_student_information model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/student/index?ok");
            }
            return Redirect("/admin/student/index?err");
        }
        #endregion

        #region 删除学生 + DelStudent()
        /// <summary>
        /// 删除学生 + DelStudent()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelStudent()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.DelBy(x => x.StudentId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限"+ex, null, "");
            }
        }
        #endregion

        //-----------------------------------------------------
        #region 设置新增和修改通用的下拉框数据 + SetDropDonwList()
        /// <summary>
        /// 设置新增和修改通用的下拉框数据
        /// </summary>
        void SetDropDonwList()
        {
            /*
             学院id对应学院
             */
            List<MODEL.qgzx_college> list = OperateContext.Current.BLLSession.Iqgzx_collegeBLL.GetListBy(x => x.CollegeId != null);
            List<SelectListItem> selectList = new List<SelectListItem>();
            foreach (MODEL.qgzx_college model in list)
            {
                selectList.Add(new SelectListItem() { Text = model.CollegeName, Value = model.CollegeId.ToString() });
            }
            ViewBag.collegeList = selectList;

            /*
             性别
             */
            List<SelectListItem> sexList = new List<SelectListItem>() { 
                new SelectListItem() { Text = "男", Value = "1"},
                new SelectListItem() { Text = "女", Value = "2"}
            };
            ViewBag.sexList = sexList;
            /*
             黑名单
             */
            List<SelectListItem> IsBlacklist = new List<SelectListItem>() { 
                new SelectListItem() { Text = "是", Value = "1"},
                new SelectListItem() { Text = "否", Value = "2"}
            };
            ViewBag.IsBlacklist = IsBlacklist;

        } 
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string StudentName = Request.QueryString["StudentName"];
            string StudentNum = Request.QueryString["StudentNum"];
            string Gender = Request.QueryString["Gender"];
            string Grade = Request.QueryString["Grade"];
            string College = Request.QueryString["College"];
            string Profession = Request.QueryString["Profession"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentId != null).ToList().Select(x => x.ToPOCO());
            if (StudentName != "")
            {
                list = list.Where(x => x.StudentName.Contains(StudentName)).ToList();
            }
            if (StudentNum != "")
            {
                list = list.Where(x => x.StudentNum.Contains(StudentNum)).ToList();
            }
            if (Gender != "")
            {
                list = list.Where(x => x.Gender.ToString().Contains(Gender)).ToList();
            }
            if (Grade != "")
            {
                list = list.Where(x => x.Grade.ToString().Contains(Grade)).ToList();
            }
            if (College != "")
            {
                list = list.Where(x => x.College.ToString().Contains(College)).ToList();
            }
            if (Profession != "")
            {
                list = list.Where(x => x.Profession.ToString().Contains(Profession)).ToList();
            }
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
